package com.ksd.api.controller;

import com.ksd.api.beans.*;
import com.ksd.api.tool.VodDataRecommend;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.common.config.PlayControlConfig;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.DateUtils;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class GetVodList extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(GetVodList.class);
    @Autowired
    private ISysMovieService sysMovieService;
    @Autowired
    private ISysBoardService sysBoardService;

    @RequestMapping(value = "/api/user/vod/getVodList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getVodList(HttpServletRequest httpServletRequest) {
        logger.info("===================获取电影分页视频数据===============");
        StringBuffer requestURL = httpServletRequest.getRequestURL();

        HttpServletUtil.getRequestParam(httpServletRequest);
        String typeDataId = httpServletRequest.getParameter("typeDataId");
        String pageSize = httpServletRequest.getParameter("pageSize");
        String keyword = httpServletRequest.getParameter("keyword");
        String cid = httpServletRequest.getParameter("cid");
        String pageNum = httpServletRequest.getParameter("pageNum");

        SysMovie sysMovie = new SysMovie();
        if (StringUtils.isNotEmpty(cid)) {
            sysMovie.setTypeData(Long.valueOf(cid));
        }
        if (StringUtils.isNotEmpty(keyword)) {
            sysMovie.setKeyWord(keyword);
        }

        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String serviceIpUrl = sysBoard.getUrl();

        startPage();
        List<SysMovie> movieList = sysMovieService.selectSysMovieList(sysMovie);
        logger.info("====分页获取所有电影视频个数======="+movieList.size()+"========cid:"+cid);

        List<VodData> vodDataList = new ArrayList<>();
        for (SysMovie movie : movieList) {
            VodData vodData = new VodData();
            vodData.setAutoid(Integer.valueOf(movie.getId().toString()));
            vodData.setTitle(movie.getTitle());
            vodData.setIs_recommend(VodDataRecommend.IS.getResult());
            vodData.setRecommend_banner(serviceIpUrl+movie.getImgUrl());
            vodData.setSource("");
            vodData.setImg_address(serviceIpUrl+movie.getImgUrl());
            vodData.setStatus(0);
            vodData.setUrl_address(serviceIpUrl+movie.getFileUrl());
            vodData.setFile_type("video");
            vodData.setCreated_at(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,movie.getCreateTime()));
            vodData.setIs_now(0);
            vodData.setUrlAddress(serviceIpUrl+movie.getFileUrl());
            vodData.setLogo(serviceIpUrl+movie.getImgUrl());
            vodData.setCat_id(movie.getTypeData().toString());
            vodDataList.add(vodData);
        }
        return AjaxResult.success(vodDataList);
    }

}